package com.framework.view.indicator.configs;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.Gravity;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.Dimension;

/**
 * 中间分隔线配置
 *
 * @author liangzx
 * @version 1.0
 * @time 2020-07-28 13:56
 **/
public class MidDividerConfig {

    /**
     * 宽度
     */
    private int width;
    /**
     * 高度
     */
    private int height;
    /**
     * 颜色
     */
    private int color;
    /**
     * 圆角矩形圆角大小
     */
    private int roundRadius;
    /**
     * 左间距
     */
    private int marginLeft;
    /**
     * 右间距
     */
    private int marginRight;
    /**
     * 上间距
     */
    private int marginTop;
    /**
     * 下间距
     */
    private int marginBottom;

    private MidDividerConfig() {
    }

    public static MidDividerConfig factory() {
        return new MidDividerConfig();
    }

    /**
     * 设置宽度
     *
     * @param width
     * @return
     */
    public MidDividerConfig setWidth(@Dimension int width) {
        this.width = width;
        return this;
    }

    /**
     * 设置高度
     *
     * @param height
     * @return
     */
    public MidDividerConfig setHeight(@Dimension int height) {
        this.height = height;
        return this;
    }

    /**
     * 设置颜色
     *
     * @param color
     * @return
     */
    public MidDividerConfig setColor(@ColorInt int color) {
        this.color = color;
        return this;
    }

    /**
     * 设置背景<br>
     * 如同时调用{@link #setColor}或{@link #setRoundRadius}，则优先使用{@link #setDrawable}的设置
     *
     * @param drawable
     * @return
     */
    public MidDividerConfig setDrawable(Drawable drawable) {
        this.drawable = drawable;
        return this;
    }

    /**
     * 设置圆角
     *
     * @param roundRadius
     * @return
     */
    public MidDividerConfig setRoundRadius(int roundRadius) {
        this.roundRadius = roundRadius;
        return this;
    }

    /**
     * 设置左间距
     *
     * @param marginLeft
     * @return
     */
    public MidDividerConfig setMarginLeft(int marginLeft) {
        this.marginLeft = marginLeft;
        return this;
    }

    /**
     * 设置右间距
     *
     * @param marginRight
     * @return
     */
    public MidDividerConfig setMarginRight(int marginRight) {
        this.marginRight = marginRight;
        return this;
    }

    /**
     * 设置上间距
     *
     * @param marginTop
     * @return
     */
    public MidDividerConfig setMarginTop(int marginTop) {
        this.marginTop = marginTop;
        return this;
    }

    /**
     * 设置下间距
     *
     * @param marginBottom
     * @return
     */
    public MidDividerConfig setMarginBottom(int marginBottom) {
        this.marginBottom = marginBottom;
        return this;
    }

    public boolean hasConfig() {
        return width != 0 && height != 0 && (color != 0 || drawable != null);
    }

    private LinearLayout.LayoutParams dividerLayoutParams;

    public LinearLayout.LayoutParams getLayoutParams() {
        if (null == dividerLayoutParams) {
            dividerLayoutParams = new LinearLayout.LayoutParams(width, height);
            dividerLayoutParams.leftMargin = marginLeft;
            dividerLayoutParams.rightMargin = marginRight;
            dividerLayoutParams.topMargin = marginTop;
            dividerLayoutParams.bottomMargin = marginBottom;
            dividerLayoutParams.gravity = Gravity.CENTER;
        }
        return dividerLayoutParams;
    }

    private Drawable drawable;

    public Drawable getDrawable() {
        if (null == drawable) {
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(color);
            if (roundRadius > 0) {
                gradientDrawable.setCornerRadius(roundRadius);
            }
            drawable = gradientDrawable;
        }
        return drawable;
    }
}
